package com.foreveross.crawl.workers;

import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.UnableToInterruptJobException;

import com.foreveross.GlobalMapOperatorBeta1;
import com.foreveross.crawl.dto.TaskModelDto;
import com.foreveross.ehcache.CacheUtil;
import com.foreveross.taskservice.common.TaskStatusEnum;
import com.foreveross.taskservice.common.bean.TaskModel;

/**
 * 清空任务缓存
 * @author luofangyi
 *
 */
public class CrawlBaseTaskClearWorker extends AbstractTaskWorker {

	public static Logger logger = Logger.getLogger(CrawlBaseTaskClearWorker.class);
	
	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {

		logger.info("清理任务开始");
		List<TaskModelDto> taskList = GlobalMapOperatorBeta1.getAllTask();
		for(TaskModelDto taskDto : taskList){
			TaskModel taskOld = taskDto.createTaskModelByRollbackTask(taskDto);
			taskOld.setStatus(TaskStatusEnum.TASK_OTHER_FAIL.getStatus());
			try {
				//回滚未做完的任务
				iCrawlNodeBaseTaskApplication.roolbackTaskQueueRecordEntity(taskOld);
			} catch (Exception e) {
				logger.error("向MQ发送任务状态失败");
			}
		}
		//清空任务
		GlobalMapOperatorBeta1.clearAllTask();
		//清空H2中任务信息
		iTaskModelEntityApplication.deleteAllTaskModel();
		iProxyChannelRelateApplication.clearAll();
		//清空错误日志缓存
		CacheUtil.getInstance().removeAllCache();
		logger.info("清理任务结束");
//		GlobalMapOperator.printTaskInfo();
	}
	
	@Override
	public void interrupt() throws UnableToInterruptJobException {
		
	}
}
